<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.tianxiabuyi.model.pojo.HospitalUser" %>
<%@ page import="com.tianxiabuyi.utils.ErrcodeUtil" %>
<%@ page import="com.tianxiabuyi.utils.UserUtil" %>
<%@ include file="/common/hospital_token.jsp"%>
<%
    Integer userId = input.getInt("user_id", token.getUid());
    HospitalUser hospitalUser = UserUtil.getUser(hospital, userId);
    if (hospitalUser == null) {
        ErrcodeUtil.setErrcode(msg, ErrcodeUtil.NO_USER);
        Tools.encryptPrint(out, msg);
        return;
    }

    boolean flag = false;

    String wechat = input.getDesc("wechat", "暂无");
    String qq = input.getDesc("qq", "暂无");
    String phone = input.getDesc("phone", "暂无");
    String avatar = input.getDesc("avatar", "暂无");
    String email = input.getDesc("email", "暂无");
    String personalSecret = input.getDesc("personal_secret", "暂无");
    int hidden = input.getInt("is_hidden", -999);

    if (!"暂无".equals(wechat)) {
        //由于input里面的setDescs方法剔除空串，需要约定特殊字符来把微信号置为""
        if (!"空".equals(wechat)) {
            if (!ValidateUtil.validateWechat(wechat)) {
                msg.setErrcodeAndErrmsg(30999, "微信号格式不正确");
                Tools.encryptPrint(out, msg);
                return;
            }
        } else {
            wechat = "";
        }
        hospitalUser.setWechat(wechat);
        flag = true;
    }
    if (!"暂无".equals(qq)) {
        if (!"空".equals(qq)) {
            if (!ValidateUtil.validateQQ(qq)) {
                msg.setErrcodeAndErrmsg(30999, "QQ号格式不正确");
                Tools.encryptPrint(out, msg);
                return;
            }
        } else {
            qq = "";
        }
        hospitalUser.setQq(qq);
        flag = true;
    }
    if (!"暂无".equals(phone)) {
        if (!ValidateUtil.validatePhone(phone)) {
            msg.setErrcodeAndErrmsg(30999, "请输入正确的手机号");
            Tools.encryptPrint(out, msg);
            return;
        }
        if (phone.equals(hospitalUser.getPhone())) {
            msg.setErrcodeAndErrmsg(ErrcodeUtil.FAILURE, "手机号与原手机号相同，请重新输入");
            Tools.encryptPrint(out, msg);
            return;
        }
        HospitalUser hospitalUserHaveThisPhone = UserUtil.getUser(phone);
        if (hospitalUserHaveThisPhone != null) {
            msg.setErrcodeAndErrmsg(ErrcodeUtil.FAILURE, "该手机号已被占用，请重新输入");
            Tools.encryptPrint(out, msg);
            return;
        }
        hospitalUser.setPhone(phone);
        flag = true;
    }
    if (!"暂无".equals(avatar)) {
        hospitalUser.setAvatar(avatar);
        flag = true;
    }
    if (!"暂无".equals(email)) {
        if (!"空".equals(email)) {
            if (!ValidateUtil.validateEmail(email)) {
                msg.setErrcodeAndErrmsg(30999, "邮箱格式不正确");
                Tools.encryptPrint(out, msg);
                return;
            }
        } else {
            email = "";
        }
        hospitalUser.setEmail(email);
        flag = true;
    }
    if (!"暂无".equals(personalSecret)) {
        hospitalUser.setPersonalSecret(personalSecret);
        flag = true;
    }
    if (-999 != hidden) {
        hospitalUser.setHidden(hidden);
        flag = true;
    }

    if (!flag) {
        ErrcodeUtil.setErrcode(msg, ErrcodeUtil.FAILURE);
        Tools.encryptPrint(out, msg);
        return;
    }

    DBTools.db("basedb").modItem(hospitalUser, userId);
    msg.setErrmsg("修改成功");
    Tools.encryptPrint(out, msg);
%>